{% extends "admin/base.html" %}

{% block title %}地标管理 - 地标和路径管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- 页面标题 -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800 page-title">地标管理</h1>
        <a href="/admin/landmarks/create" class="btn btn-primary">
            <i class="bi bi-plus-circle me-1"></i> 添加地标
        </a>
    </div>

    <!-- 搜索和过滤区域 -->
    <div class="card mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="input-group">
                        <input type="text" class="form-control bg-light border-0 small" placeholder="搜索地标..." id="search-input">
                        <button class="btn btn-primary" type="button">
                            <i class="bi bi-search"></i>
                        </button>
                    </div>
                </div>
                <div class="col-md-6 text-end">
                    <div class="btn-group" role="group">
                        <button type="button" class="btn btn-outline-secondary active" id="view-table">
                            <i class="bi bi-table"></i>
                        </button>
                        <button type="button" class="btn btn-outline-secondary" id="view-card">
                            <i class="bi bi-grid"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 地标列表 -->
    <div id="landmarks-table-view">
        <div class="card">
            <div class="card-header">
                <h6 class="m-0 font-weight-bold text-primary">地标列表</h6>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover" id="landmarks-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>名称</th>
                                <th>坐标</th>
                                <th>简介</th>
                                <th>媒体</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for landmark in landmarks %}
                            <tr>
                                <td>{{ landmark.id }}</td>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="ms-2">
                                            <div class="fw-bold">{{ landmark.name }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td>{{ landmark.lat }}, {{ landmark.lng }}</td>
                                <td>{{ landmark.summary }}</td>
                                <td>
                                    {% if landmark.media %}
                                        <div class="d-flex flex-wrap">
                                            {% for media in landmark.media %}
                                                <img src="/{{ media }}" class="preview-image me-1 mb-1" alt="媒体文件">
                                            {% endfor %}
                                        </div>
                                    {% else %}
                                        <span class="text-muted">无</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <div class="btn-group">
                                        <a href="/admin/landmarks/{{ landmark.id }}/edit" class="btn btn-sm btn-outline-primary">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        <a href="/admin/landmarks/{{ landmark.id }}/delete" class="btn btn-sm btn-outline-danger"
                                           onclick="return confirm('确定要删除这个地标吗？');">
                                            <i class="bi bi-trash"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- 卡片视图 -->
    <div id="landmarks-card-view" style="display: none;">
        <div class="row">
            {% for landmark in landmarks %}
            <div class="col-xl-4 col-md-6 mb-4 landmark-card">
                <div class="card h-100">
                    <div class="card-body">
                        <div class="row no-gutters align-items-center">
                            <div class="col mr-2">
                                <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">地标</div>
                                <div class="h5 mb-0 font-weight-bold text-gray-800">{{ landmark.name }}</div>
                            </div>
                            <div class="col-auto">
                                <i class="bi bi-geo-alt-fill text-primary fa-2x"></i>
                            </div>
                        </div>

                        <div class="mt-3">
                            <p class="text-muted">{{ landmark.summary }}</p>

                            {% if landmark.media %}
                            <div class="mb-3">
                                <div class="d-flex flex-wrap">
                                    {% for media in landmark.media[:2] %}  <!-- 只显示前两张图片 -->
                                    <img src="/{{ media }}" class="preview-image me-1 mb-1" alt="媒体文件">
                                    {% endfor %}
                                    {% if landmark.media|length > 2 %}
                                    <div class="preview-image d-flex align-items-center justify-content-center bg-light">
                                        <span>+{{ landmark.media|length - 2 }}</span>
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            {% endif %}

                            <div class="d-flex justify-content-between align-items-center">
                                <small class="text-muted">{{ landmark.lat }}, {{ landmark.lng }}</small>
                                <div class="btn-group">
                                    <a href="/admin/landmarks/{{ landmark.id }}/edit" class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="/admin/landmarks/{{ landmark.id }}/delete" class="btn btn-sm btn-outline-danger"
                                       onclick="return confirm('确定要删除这个地标吗？');">
                                        <i class="bi bi-trash"></i>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // 视图切换
        $('#view-table').click(function() {
            $('#landmarks-table-view').show();
            $('#landmarks-card-view').hide();
            $(this).addClass('active');
            $('#view-card').removeClass('active');
        });

        $('#view-card').click(function() {
            $('#landmarks-table-view').hide();
            $('#landmarks-card-view').show();
            $(this).addClass('active');
            $('#view-table').removeClass('active');
        });

        // 搜索功能
        $('#search-input').on('keyup', function() {
            var value = $(this).val().toLowerCase();
            $("#landmarks-table tbody tr").filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });

            $(".landmark-card").filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });
</script>
{% endblock %}
